<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Using the Factory to Create a Log - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.log.factory.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.log.factory.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.log.filters.html">Filters</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.log.html">Zend_Log</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.mail.html">Zend_Mail</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.log.factory" class="section"><div class="info"><h1 class="title">Using the Factory to Create a Log</h1></div>
    

    <p class="para">
        In addition to direct instantiation, you may also use the static
         <span class="methodname">factory()</span> method to instantiate a Log instance, as well as to
        configure attached writers and their filters. Using the factory, you can attach zero or
        more writers. Configuration may be passed as either an array or a
        <span class="classname">Zend_Config</span> instance.
    </p>

    <p class="para">
        As an example:
    </p>

    <pre class="programlisting brush: php">
$logger = Zend_Log::factory(array(
    &#039;timestampFormat&#039; =&gt; &#039;Y-m-d&#039;,
    array(
        &#039;writerName&#039;   =&gt; &#039;Stream&#039;,
        &#039;writerParams&#039; =&gt; array(
            &#039;stream&#039;   =&gt; &#039;/tmp/zend.log&#039;,
        ),
        &#039;formatterName&#039; =&gt; &#039;Simple&#039;,
        &#039;formatterParams&#039; =&gt; array(
            &#039;format&#039;   =&gt; &#039;%timestamp%: %message% -- %info%&#039;,
        ),
        &#039;filterName&#039;   =&gt; &#039;Priority&#039;,
        &#039;filterParams&#039; =&gt; array(
            &#039;priority&#039; =&gt; Zend_Log::WARN,
        ),
    ),
    array(
        &#039;writerName&#039;   =&gt; &#039;Firebug&#039;,
        &#039;filterName&#039;   =&gt; &#039;Priority&#039;,
        &#039;filterParams&#039; =&gt; array(
            &#039;priority&#039; =&gt; Zend_Log::INFO,
        ),
    ),
));
</pre>


    <p class="para">
        The above will instantiate a logger with two writers, one for writing to a local file,
        another for sending data to Firebug. Each has an attached priority filter, with different
        maximum priorities.
    </p>

    <p class="para">
        By default, events are logged with the ISO 8601 date format. You can choose your own format
        with the option <em class="emphasis">timestampFormat</em>.
    </p>

    <p class="para">
        Each writer can be defined with the following keys:
    </p>

    <dl>

        <dt>

            <dt class="term">writerName (required)</dt>

            <dd>

                <p class="para">
                    The &quot;short&quot; name of a log writer; the name of the log writer minus the leading
                    class prefix/namespace. See the &quot;writerNamespace&quot; entry below for more details.
                    Examples: &quot;Mock&quot;, &quot;Stream&quot;, &quot;Firebug&quot;.
                </p>
            </dd>

        </dt>


        <dt>

            <dt class="term">writerParams (optional)</dt>

            <dd>

                <p class="para">
                    An associative array of parameters to use when instantiating the log writer.
                    Each log writer&#039;s  <span class="methodname">factory()</span> method will map these to
                    constructor arguments, as noted below.
                </p>
            </dd>

        </dt>


        <dt>

            <dt class="term">writerNamespace (optional)</dt>

            <dd>

                <p class="para">
                    The class prefix/namespace to use when constructing the final log writer
                    classname. By default, if this is not provided, &quot;Zend_Log_Writer&quot; is assumed;
                    however, you can pass your own namespace if you are using a custom log writer.
                </p>
            </dd>

        </dt>


        <dt>

            <dt class="term">formatterName (optional)</dt>

            <dd>

                <p class="para">
                    The &quot;short&quot; name of a formatter to use with the given log writer; the name of the
                    formatter minus the leading class prefix/namespace. See the &quot;formatterNamespace&quot; 
                    entry below for more details. Examples: &quot;Simple&quot;, &quot;Xml&quot;.
                </p>
            </dd>

        </dt>


        <dt>

            <dt class="term">formatterParams (optional)</dt>

            <dd>

                <p class="para">
                    An associative array of parameters to use when instantiating the log formatter.
                    Each log formatter&#039;s  <span class="methodname">factory()</span> method will map these to
                    constructor arguments, as noted below.
                </p>
            </dd>

        </dt>


        <dt>

            <dt class="term">formatterNamespace (optional)</dt>

            <dd>

                <p class="para">
                    The class prefix/namespace to use when constructing the final log formatter
                    classname. By default, if this is not provided, &quot;Zend_Log_Formatter&quot; is assumed;
                    however, you can pass your own namespace if you are using a custom log formatter.
                </p>
            </dd>

        </dt>


        <dt>

            <dt class="term">filterName (optional)</dt>

            <dd>

                <p class="para">
                    The &quot;short&quot; name of a filter to use with the given log writer; the name of the
                    filter minus the leading class prefix/namespace. See the &quot;filterNamespace&quot; entry
                    below for more details. Examples: &quot;Message&quot;, &quot;Priority&quot;.
                </p>
            </dd>

        </dt>


        <dt>

            <dt class="term">filterParams (optional)</dt>

            <dd>

                <p class="para">
                    An associative array of parameters to use when instantiating the log filter.
                    Each log filter&#039;s  <span class="methodname">factory()</span> method will map these to
                    constructor arguments, as noted below.
                </p>
            </dd>

        </dt>


        <dt>

            <dt class="term">filterNamespace (optional)</dt>

            <dd>

                <p class="para">
                    The class prefix/namespace to use when constructing the final log filter
                    classname. By default, if this is not provided, &quot;Zend_Log_Filter&quot; is assumed;
                    however, you can pass your own namespace if you are using a custom log filter.
                </p>
            </dd>

        </dt>

    </dl>


    <p class="para">
        Each writer and each filter has specific options.
    </p>

    <div class="section" id="zend.log.factory.writer-options"><div class="info"><h1 class="title">Writer Options</h1></div>
        

        <div class="section" id="zend.log.factory.writer-options.db"><div class="info"><h1 class="title">Zend_Log_Writer_Db Options</h1></div>
            

            <dl>

                <dt>

                    <dt class="term">db</dt>

                    <dd>

                        <p class="para">
                            A <span class="classname">Zend_Db_Adapter</span> instance.
                        </p>
                    </dd>

                </dt>


                <dt>

                    <dt class="term">table</dt>

                    <dd>

                        <p class="para">
                            The name of the table in the RDBMS that will contain log entries.
                        </p>
                    </dd>

                </dt>


                <dt>

                    <dt class="term">columnMap</dt>

                    <dd>

                        <p class="para">
                            An associative array mapping database table column names to log event
                            fields.
                        </p>
                    </dd>

                </dt>

            </dl>

        </div>

        <div class="section" id="zend.log.factory.writer-options.firebug"><div class="info"><h1 class="title">Zend_Log_Writer_Firebug Options</h1></div>
            

            <p class="para">
                This log writer takes no options; any provided will be ignored.
            </p>
        </div>

        <div class="section" id="zend.log.factory.writer-options.mail"><div class="info"><h1 class="title">Zend_Log_Writer_Mail Options</h1></div>
            

            <table id="zend.log.factory.writer-options.mail.table" class="doctable table"><div class="info"><caption><b>Zend_Log_Writer_Mail Options</b></caption></div>
                

                
                    <thead valign="middle">
                        <tr valign="middle">
                            <th>Option</th>
                            <th>Data Type</th>
                            <th>Default Value</th>
                            <th>Description</th>
                        </tr>

                    </thead>


                    <tbody valign="middle" class="tbody">
                        <tr valign="middle">
                            <td align="left"><em class="emphasis">mail</em></td>
                            <td align="left"><span class="type">String</span></td>
                            <td align="left"><span class="classname">Zend_Mail</span></td>

                            <td align="left">
                                An <span class="classname">Zend_Mail</span> instance
                            </td>
                        </tr>


                        <tr valign="middle">
                            <td align="left"><em class="emphasis">charset</em></td>
                            <td align="left"><span class="type">String</span></td>
                            <td align="left">iso-8859-1</td>

                            <td align="left">
                                Charset of the mail
                            </td>
                        </tr>


                        <tr valign="middle">
                            <td align="left"><em class="emphasis">from</em></td>
                            <td align="left"><span class="type">String</span> or <span class="type">Array</span></td>
                            <td align="left"><b><tt>NULL</tt></b></td>

                            <td align="left">
                                Sender of the mail

                                The parameters for <span class="type">Array</span> type are :
                                <ul class="itemizedlist">
                                    <li class="listitem">
                                        <p class="para">
                                            <span class="property">email</span> : address of sender
                                        </p>
                                    </li>

                                    <li class="listitem">
                                        <p class="para">
                                            <span class="property">name</span> : name of sender
                                        </p>
                                    </li>
                                </ul>
                            </td>
                        </tr>


                        <tr valign="middle">
                            <td align="left"><em class="emphasis">to</em></td>
                            <td align="left"><span class="type">String</span> or <span class="type">Array</span></td>
                            <td align="left"><b><tt>NULL</tt></b></td>

                            <td align="left">
                                Recipient(s) of the mail
                            </td>
                        </tr>


                        <tr valign="middle">
                            <td align="left"><em class="emphasis">cc</em></td>
                            <td align="left"><span class="type">String</span> or <span class="type">Array</span></td>
                            <td align="left"><b><tt>NULL</tt></b></td>

                            <td align="left">
                                Carbon copy recipient(s) of the mail
                            </td>
                        </tr>


                        <tr valign="middle">
                            <td align="left"><em class="emphasis">bcc</em></td>
                            <td align="left"><span class="type">String</span> or <span class="type">Array</span></td>
                            <td align="left"><b><tt>NULL</tt></b></td>

                            <td align="left">
                                Blind carbon copy recipient(s) of the mail
                            </td>
                        </tr>


                        <tr valign="middle">
                            <td align="left"><em class="emphasis">subject</em></td>
                            <td align="left"><span class="type">String</span></td>
                            <td align="left"><b><tt>NULL</tt></b></td>

                            <td align="left">
                                Subject of the mail
                            </td>
                        </tr>


                        <tr valign="middle">
                            <td align="left"><em class="emphasis">subjectPrependText</em></td>
                            <td align="left"><span class="type">String</span></td>
                            <td align="left"><b><tt>NULL</tt></b></td>

                            <td align="left">
                                A summary of number of errors per priority is appended to the
                                subject of the mail
                            </td>
                        </tr>


                        <tr valign="middle">
                            <td align="left"><em class="emphasis">layout</em></td>
                            <td align="left"><span class="type">String</span></td>
                            <td align="left"><b><tt>NULL</tt></b></td>

                            <td align="left">
                                An <span class="classname">Zend_Layout</span> instance
                            </td>
                        </tr>


                        <tr valign="middle">
                            <td align="left"><em class="emphasis">layoutOptions</em></td>
                            <td align="left"><span class="type">Array</span></td>
                            <td align="left"><b><tt>NULL</tt></b></td>

                            <td align="left">
                                See the section <a href="zend.layout.options.html" class="xref">Zend_Layout Configuration Options</a>
                            </td>
                        </tr>


                        <tr valign="middle">
                            <td align="left"><em class="emphasis">layoutFormatter</em></td>
                            <td align="left"><span class="type">String</span></td>
                            <td align="left"><b><tt>NULL</tt></b></td>

                            <td align="left">
                                An <span class="classname">Zend_Log_Formatter_Interface</span> instance
                            </td>
                        </tr>

                    </tbody>
                
            </table>

        </div>

        <div class="section" id="zend.log.factory.writer-options.mock"><div class="info"><h1 class="title">Zend_Log_Writer_Mock Options</h1></div>
            

            <p class="para">
                This log writer takes no options; any provided will be ignored.
            </p>
        </div>

        <div class="section" id="zend.log.factory.writer-options.null"><div class="info"><h1 class="title">Zend_Log_Writer_Null Options</h1></div>
            

            <p class="para">
                This log writer takes no options; any provided will be ignored.
            </p>
        </div>

        <div class="section" id="zend.log.factory.writer-options.stream"><div class="info"><h1 class="title">Zend_Log_Writer_Stream Options</h1></div>
            

            <dl>

                <dt>

                    <dt class="term">stream|url</dt>

                    <dd>

                        <p class="para">
                            A valid <acronym class="acronym">PHP</acronym> stream identifier to which to log.
                        </p>
                    </dd>

                </dt>


                <dt>

                    <dt class="term">mode</dt>

                    <dd>

                        <p class="para">
                            The I/O mode with which to log; defaults to &quot;a&quot;, for &quot;append&quot;.
                        </p>
                    </dd>

                </dt>

            </dl>

        </div>

        <div class="section" id="zend.log.factory.writer-options.syslog"><div class="info"><h1 class="title">Zend_Log_Writer_Syslog Options</h1></div>
            

            <dl>

                <dt>

                    <dt class="term">application</dt>

                    <dd>

                        <p class="para">
                            Application name used by the syslog writer.
                        </p>
                    </dd>

                </dt>


                <dt>

                    <dt class="term">facility</dt>

                    <dd>

                        <p class="para">
                            Facility used by the syslog writer.
                        </p>
                    </dd>

                </dt>

            </dl>

        </div>

        <div class="section" id="zend.log.factory.writer-options.zendmonitor"><div class="info"><h1 class="title">Zend_Log_Writer_ZendMonitor Options</h1></div>
            

            <p class="para">
                This log writer takes no options; any provided will be ignored.
            </p>
        </div>
    </div>

    <div class="section" id="zend.log.factory.filter-options"><div class="info"><h1 class="title">Filter Options</h1></div>
        

        <div class="section" id="zend.log.factory.filter-options.message"><div class="info"><h1 class="title">Zend_Log_Filter_Message Options</h1></div>
            

            <dl>

                <dt>

                    <dt class="term">regexp</dt>

                    <dd>

                        <p class="para">
                            Regular expression that must be matched in order to log a message.
                        </p>
                    </dd>

                </dt>

            </dl>

        </div>

        <div class="section" id="zend.log.factory.filter-options.priority"><div class="info"><h1 class="title">Zend_Log_Filter_Priority Options</h1></div>
            

            <dl>

                <dt>

                    <dt class="term">priority</dt>

                    <dd>

                        <p class="para">
                            The maximum priority level by which messages will be logged.
                        </p>
                    </dd>

                </dt>


                <dt>

                    <dt class="term">operator</dt>

                    <dd>

                        <p class="para">
                            The comparison operator by which to do priority comparisons; defaults to
                            &quot;&lt;=&quot;.
                        </p>
                    </dd>

                </dt>

            </dl>

        </div>

        <div class="section" id="zend.log.factory.filter-options.suppress"><div class="info"><h1 class="title">Zend_Log_Filter_Suppress Options</h1></div>
            

            <p class="para">
                This log filter takes no options; any provided will be ignored.
            </p>
        </div>
    </div>

    <div class="section" id="zend.log.factory.custom"><div class="info"><h1 class="title">Creating Configurable Writers and Filters</h1></div>
        

        <p class="para">
            If you find yourself needing to write your own log writers and/or filters, you can make
            them compatible with  <span class="methodname">Zend_Log::factory()</span> very easily.
        </p>

        <p class="para">
            At the minimum, you need to implement
            <span class="interfacename">Zend_Log_FactoryInterface</span>, which expects a static
             <span class="methodname">factory()</span> method that accepts a single argument,
            <var class="varname">$config</var>, which may be either an array or
            <span class="classname">Zend_Config</span> object. If your log writer extends
            <span class="classname">Zend_Log_Writer_Abstract</span>, or your log filter extends
            <span class="classname">Zend_Log_Filter_Abstract</span>, you will pick this up for free.
        </p>

        <p class="para">
            Then, simply define mappings between the accepted configuration and any constructor
            arguments. As an example:
        </p>

        <pre class="programlisting brush: php">
class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
{
    public function __construct($bar, $baz)
    {
        // ...
    }

    public static function factory($config)
    {
        if ($config instanceof Zend_Config) {
            $config = $config-&gt;toArray();
        }
        if (!is_array($config)) {
            throw new Exception(
                &#039;factory expects an array or Zend_Config instance&#039;
            );
        }

        $default = array(
            &#039;bar&#039; =&gt; null,
            &#039;baz&#039; =&gt; null,
        );
        $config = array_merge($default, $config);

        return new self(
            $config[&#039;bar&#039;],
            $config[&#039;baz&#039;]
        );
    }
}
</pre>


        <p class="para">
            Alternately, you could call appropriate setters after instantiation, but prior to
            returning the instance:
        </p>

        <pre class="programlisting brush: php">
class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
{
    public function __construct($bar = null, $baz = null)
    {
        // ...
    }

    public function setBar($value)
    {
        // ...
    }

    public function setBaz($value)
    {
        // ...
    }

    public static function factory($config)
    {
        if ($config instanceof Zend_Config) {
            $config = $config-&gt;toArray();
        }
        if (!is_array($config)) {
            throw new Exception(
                &#039;factory expects an array or Zend_Config instance&#039;
            );
        }

        $writer = new self();
        if (isset($config[&#039;bar&#039;])) {
            $writer-&gt;setBar($config[&#039;bar&#039;]);
        }
        if (isset($config[&#039;baz&#039;])) {
            $writer-&gt;setBaz($config[&#039;baz&#039;]);
        }
        return $writer;
    }
}
</pre>

    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.log.filters.html">Filters</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.log.html">Zend_Log</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.mail.html">Zend_Mail</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.log.html">Zend_Log</a></li>
  <li><a href="zend.log.overview.html">Overview</a></li>
  <li><a href="zend.log.writers.html">Writers</a></li>
  <li><a href="zend.log.formatters.html">Formatters</a></li>
  <li><a href="zend.log.filters.html">Filters</a></li>
  <li class="active"><a href="zend.log.factory.html">Using the Factory to Create a Log</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>